using AppFiniteStateMachine;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;


namespace AppSoldierState
{

    public class SoldierInitState : IState
    {
        private SoldierStateManager manager;
        private SoldierParams parameter;

        private float duration_time = 0f;
        private float transition_time = 3f;
        
        public SoldierInitState(SoldierStateManager manager)
        {
            this.manager = manager;
            this.parameter = manager.parameter;
        }
        
        public void OnEnter()
        {
            duration_time = 0f;
            BdUtils.log($"--- {GetType().Name}.OnEnter");
            
            parameter.init_obj.SetActive(true);
        }

        public void OnUpdate()
        {
            duration_time += Time.deltaTime;
            if (duration_time > transition_time)
                manager.TransitionState(SoldierStateEnum.Walk);
        }

        public void OnExit()
        {
            BdUtils.log($"--- {GetType().Name}.OnExit");
            
            parameter.init_obj.SetActive(false);
        }
    }
    
    
    public class SoldierWalkState : IState
    {
        private SoldierStateManager manager;
        private SoldierParams parameter;

        private float duration_time = 0f;
        // private float transition_time = 3f;

        private float pos_right => parameter.m_init_position.x + parameter.distance;
        private float pos_left => parameter.m_init_position.x - parameter.distance;
        
        public SoldierWalkState(SoldierStateManager manager)
        {
            this.manager = manager;
            this.parameter = manager.parameter;
        }
        
        public void OnEnter()
        {
            BdUtils.log($"--- {GetType().Name}.OnEnter");

            duration_time = 0f;
            
            parameter.walk_obj.SetActive(true);

            // pos_left = parameter.m_init_position.x - parameter.distance;
            // pos_right = parameter.m_init_position.x + parameter.distance;

            // parameter.soldier_obj;
        }
        
        // private Vector3 soldier_pos => parameter.soldier_obj.transform.position;

        private Vector3 soldier_pos
        {
            get
            {
                return parameter.soldier_obj.transform.position;
            }
            set
            {
                parameter.soldier_obj.transform.position = value;
            }
        }
        
        private bool is_in_distance_range()
        {
            return soldier_pos.x > pos_left && soldier_pos.x < pos_right;
        }

        private int get_soldier_direction()
        {
            if (soldier_pos.x >= pos_right)
            {
                return -1;
            }
            else if (soldier_pos.x <= pos_left)
            {
                return 1;
            }
            else
            {
                // 随便抽取一个
                return BdUtils.get_random_number(0, 1 + 1) * 2 - 1;
            }
        }

        public void OnUpdate()
        {
            duration_time += Time.deltaTime;
            // if (duration_time > transition_time)
            //     manager.TransitionState(SoldierStateEnum.Walk);
            
            if (!is_in_distance_range())
            {
                parameter.direction = get_soldier_direction();
            }
            
            var delta_x = parameter.speed * parameter.direction * Time.deltaTime;
            soldier_pos = new Vector3(soldier_pos.x + delta_x, soldier_pos.y, soldier_pos.z);
        }

        public void OnExit()
        {
            BdUtils.log($"--- {GetType().Name}.OnExit");
            
            parameter.walk_obj.SetActive(false);
        }
    }
}